package com.mazaiting.api.decoder;

import com.mazaiting.common.core.domain.result.Result;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.runtime.FeignBizException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * openfeign全局异常处理
 *
 * @author mazaiting
 * @create_time 2022/9/18 17:49
 */
@Slf4j
@ResponseBody
@ControllerAdvice
@ConditionalOnMissingBean(FeignExceptionAdvice.class)
public class FeignExceptionAdvice {

    /**
     * openFeign 调用异常
     *
     * @param e 异常
     * @return 结果
     */
    @ExceptionHandler(value = FeignException.class)
    public Result<?> exceptionHandler(FeignException e) {
        e.printStackTrace();
        log.error("FeignException: {}", e.getMessage());
        return Result.failed(ResultCode.FEIGN_DEGRADATION, e.getMessage());
    }

    /**
     * openFeign 调用异常
     *
     * @param e 异常
     * @return 结果
     */
    @ExceptionHandler(value = FeignBizException.class)
    public Result<?> exceptionHandler(FeignBizException e) {
        e.printStackTrace();
        log.error("FeignBizException: {}", e.toString());
        return Result.failed(e.getCode(), e.getMessage());
    }

}
